1977B - Binary Colouring - CodeForces Solution


bitmasks constructive algorithms math

Please click on ads to support us..

Python Code:

def check_highest_2(x):
    i = 31
    while i >= 0:
        if 2 ** i <= x:
            return i
        i -= 1

def recur(x, ans, isminus):
        if x <= 0:
        return True
    temp = check_highest_2(x)
    done = True
    done2 = True
    for num in ans:
        if num[1] == temp+1:
            done = False
        if num[1] == temp+2 or num[1] == temp + 1:
            done2 = False
    if done:   
        if isminus:
            ans.append((-1, temp))
        else:
            ans.append((1, temp))
        
        if recur(x - 2 ** temp, ans, isminus) == True:
            return True
        ans.pop()
    if done2:
        if isminus:
            ans.append((-1, temp+1))
        else:
            ans.append((1, temp+1))
        
        if recur(2 ** (temp+1) - x, ans, not isminus) == True:
            return True
        ans.pop()
    return False

    
def solve():
    ans = []
    x = int(input())

    recur(x, ans, False)
    temp_ans = [0] * (32)
    for num in ans:
        temp_ans[num[1]] = num[0]
    print(32)
    print(* temp_ans)

for i in range(int(input())):
    solve()


Comments

Submit
0 Comments
More Questions

1196C - Robot Breakout
373A - Collecting Beats is Fun
965A - Paper Airplanes
863E - Turn Off The TV
630E - A rectangle
1104A - Splitting into digits
19C - Deletion of Repeats
1550B - Maximum Cost Deletion
1693A - Directional Increase
735D - Taxes
989A - A Blend of Springtime
339C - Xenia and Weights
608A - Saitama Destroys Hotel
1342C - Yet Another Counting Problem
548A - Mike and Fax
109A - Lucky Sum of Digits
864C - Bus
626B - Cards
1221A - 2048 Game
1374D - Zero Remainder Array
1567C - Carrying Conundrum
1029C - Maximal Intersection
922C - Cave Painting
811C - Vladik and Memorable Trip
1589C - Two Arrays
1510K - King's Task
126B - Password
462A - Appleman and Easy Task
839C - Journey
622A - Infinite Sequence